package com.lintesis.lintesisview.webgui.permiso;

import java.io.Serializable;

import org.zkoss.zul.Listheader;
import org.zkoss.zul.Textbox;

import com.lintesis.lintesismodel.common.hibernate.BussinessObject;
import com.lintesis.lintesismodel.common.hibernate.GenericDAO;
import com.lintesis.lintesismodel.common.hibernate.HibernateSearchObject;
import com.lintesis.lintesismodel.users.UsrRight;
import com.lintesis.lintesisview.common.ListCtrl;
import com.lintesis.lintesisview.common.menu.UserWorkspace;
import com.lintesis.lintesisview.common.util.pagging.PagedListWrapper;

import com.trg.search.Filter;
import com.trg.search.SearchResult;


public class PermisoListCtrl extends ListCtrl<UsrRight> implements Serializable{
	private static final long serialVersionUID = 5759588897352850321L;
	Textbox tbNombre;
	Listheader lhNombre;

	protected GenericDAO<UsrRight> genericDAO = new GenericDAO<UsrRight>(UserWorkspace.getInstance().gethSession());

	@Override
	protected void doDefineOrders()
	{
		orders.put("name", lhNombre);
	}

	public void doSearch()
	{
		getHibernateSearchObject().clearFilters();

//		Filter filter = Filter.or(new Filter("key", null, Filter.OP_EMPTY), 
//									Filter.and(Filter.not(new Filter("key", "%dialog%", Filter.OP_ILIKE)), 
//									Filter.not(new Filter("key", "%list%", Filter.OP_ILIKE)),
//									Filter.not(new Filter("key", "%menu%", Filter.OP_ILIKE)))
//									);
		getHibernateSearchObject().addFilter(new Filter("key", null, Filter.OP_EMPTY));

		for (String key : filters.keySet())
		{
			if (filters.get(key) != null && !filters.get(key).equals("%%"))
				if (filters.get(key) instanceof String)
					getHibernateSearchObject().addFilterILike(key, filters.get(key).toString());
				else
					getHibernateSearchObject().addFilterEqual(key, filters.get(key));

		}
		for (String key : defaultOrders.keySet())
		{
			getHibernateSearchObject().addSort(key, defaultOrders.get(key));
		}
		SearchResult<UsrRight> initialList = getGenericDao().getSRBySearchObject(getHibernateSearchObject(), 0, countRows);
		pagingList.setPageSize(countRows);
		pagingList.setDetailed(true);

		setListModelEntity(new PagedListWrapper<UsrRight>(listBoxEntity, pagingList, initialList, getHibernateSearchObject()));
		binder.loadAll();

	}

	@Override
	protected String getABMDialogURL()
	{
		return "/pages/permiso/permisoDialog.zul";
	}

	@Override
	protected GenericDAO<UsrRight> getGenericDao()
	{
		return genericDAO;
	}

	@Override
	protected HibernateSearchObject<UsrRight> getNewHibernateSearchObject()
	{
		return new HibernateSearchObject<UsrRight>(UsrRight.class);
	}

	@Override
	public PagedListWrapper<UsrRight> getListModelEntity()
	{
		return listModelEntity;
	}

	@Override
	protected BussinessObject getNewEntidad()
	{
		return new UsrRight();
	}

	@Override
	public void setListModelEntity(PagedListWrapper<UsrRight> listModelEntidad)
	{
		this.listModelEntity = listModelEntidad;

	}

	@Override
	protected void setSearchFilters()
	{
		filters.clear();
		filters.put("name", "%" + tbNombre.getValue() + "%");

	}

	@Override
	protected void setDescripcionLarga()
	{
		// String descripcion = "";
		// UsrRight permiso = getSelectedEntidad();
		//
		// if (permiso.getChildren() != null &&
		// !permiso.getChildren().isEmpty())
		// {
		// descripcion = "Permisos Hijos: ";
		// for (UsrRight permisoHijo : permiso.getChildren())
		// {
		// descripcion += permisoHijo.getName() + ", ";
		// }
		// descripcion = descripcion.substring(0, descripcion.lastIndexOf(","));
		// }
		// lbDescripcionLarga.setValue(descripcion);
	}

	@Override
	public void doSetSearchComponents()
	{
		searchComponents.add(tbNombre);
	}

	@Override
	public String getEntityName()
	{
		return "UsrRight";
	}

	@Override
	protected void setDefaultFilters() {
		// TODO Auto-generated method stub
	}
	
}
